import { createAction } from 'redux-actions'
import { delJson, body, putJson, postJson, check, parseJSON, checkStatus, data, getJson } from './util'
import fetch from 'isomorphic-fetch'


export 
const updateRewards = createAction('update rewards', (arg, cb) =>
  fetch('/rewards/reward-quota-setting', {
    ...postJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

export 
const fetchRewards = createAction('fetch rewards', (arg, cb) =>
  fetch('/rewards/reward-quota-setting', getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then((res) => {
      return res.data;
    })
)

export 
const addMedal = createAction('add medal', (arg, cb) =>
  fetch('/rewards/reward-medal-setting-batch', {
    ...postJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

export 
const updateMedal = createAction('update medal', (arg, cb) =>
  fetch('/rewards/reward-medal-setting-batch', {
    ...putJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

export 
const delMedal = createAction('del medal', (arg, cb) =>
  fetch('/rewards/reward-medal-setting-batch', {
    ...delJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

export 
const fetchMedal = createAction('fetch medal', (arg, cb) =>
  fetch('/rewards/reward-medal-setting', getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then((res) => {
      return res.data;
    })
)


export 
const fetchQuotaInfo = createAction('fetch quota info', ( cb) =>
  fetch(`/rewards/reward-quota-info`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then((res) => {
      cb && cb();
      return res.data;
    })
)



export 
const fetchRewardsById = createAction('fetch rewards by id', (id, cb) =>
  fetch(`/rewards/reward-received/${id}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then((res) => {
      cb && cb();
      return {
        id,
        data: res.data,
      };
    })
)

